package com.muyu.fence.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.fence.domain.database.ElectronicFence;
import com.muyu.fence.domain.database.ElectronicFenceGroup;
import com.muyu.fence.domain.req.FenceWayReq;
import com.muyu.fence.domain.req.ElectroicFenceAddReq;
import com.muyu.fence.domain.req.ElectroicFenceListReq;
import com.muyu.fence.domain.req.ElectroicFenceUpdReq;
import com.muyu.fence.domain.resp.ElectronicFenceResp;
import com.muyu.fence.service.ElectronicFenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：yuping
 * @Package：com.muyu.fence.controller
 * @Project：cloud-server
 * @name：FenceEtlController
 * @Date：2024/9/17 16:28
 */
@Log4j2
@Tag(name = "电子围栏")
@RestController
@RequestMapping("/fence")
@RequiredArgsConstructor
public class ElectronicFenceController {

    @Autowired
    private ElectronicFenceService electronicFenceService;


    @PostMapping("/fenceArray")
    @Operation(description = "查询所有可用的围栏")
    public Result<List<ElectronicFenceResp>> fenceArray() {
        System.out.println("=====>" + "hgfvhgjy");
        return Result.success(electronicFenceService.fenceArray());
    }

    @PostMapping("/fenceselectList")
    @Operation(description = "列表")
    public Result<List<ElectronicFenceResp>> fenceselectList(@RequestBody ElectroicFenceListReq electroicFenceListReq) {
        System.out.println("=====>" + "hgfvhgjy");
        return Result.success(electronicFenceService.fenceselectList(electroicFenceListReq));
    }

    @PostMapping("/add")
    @Operation(description = "添加")
    public Result AddFence(@RequestBody ElectroicFenceAddReq electroicFenceAddReq) {

        electronicFenceService.unquireFence(electroicFenceAddReq.getName());
        electronicFenceService.AddFence(electroicFenceAddReq);

        return Result.success();
    }

    @PostMapping("/upd/{id}")
    @Operation(description = "修改")
    public Result UpdFence(@PathVariable("id") Long id, @RequestBody ElectroicFenceUpdReq electroicFenceUpdReq) {

        electronicFenceService.updateById(ElectronicFence.buildByElectronicUpd(electroicFenceUpdReq, () -> id));

        return Result.success();
    }

    @PostMapping("/findElectronicByid/{id}")
    @Operation(description = "通过id回显围栏信息")
    public Result<ElectronicFence> findElectronicByid(@PathVariable("id") Long id) {

        ElectronicFence  electronicFence= electronicFenceService.findElectronicByid(id);
        return Result.success(electronicFence);

    }

//    @PostMapping("/delElectronById/{id}")
//    @Operation(description = "通过id删除围栏")
//    public Result delElectronById(@PathVariable("id") Long id) {
//
//        electronicFenceService.delElectronById(id);
//
//        return Result.success();
//    }

    /**
     * 电子围栏表信息表删除
     * @param electronicFenceId 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/{electronicFenceId}")
    @Operation(summary = "电子围栏表信息表删除",
            description = "通过ID删除电子围栏表信息")
    public Result<String> remove(@PathVariable("electronicFenceId") Long electronicFenceId)
    {
        boolean b = electronicFenceService.removeById(electronicFenceId);
        return Result.success(null, "操作成功");
    }

    @PostMapping("/open/{id}")
    @Operation(description = "开启围栏")
    public Result openFence(@PathVariable("id") Long id) {

        electronicFenceService.openFence(id);

        return Result.success();
    }

    @PostMapping("/close/{id}")
    @Operation(description = "关闭围栏")
    public Result closeFence(@PathVariable("id") Long id) {

        electronicFenceService.closeFence(id);

        return Result.success();
    }

    @PostMapping("/setFenceWay")
    @Operation(description = "设置电子围栏的位置")
    public Result setFenceWay(@RequestBody FenceWayReq fenceWayReq) {

        Long id = fenceWayReq.getId();

        String longitudeLatitude = fenceWayReq.getLongitudeLatitude();

        log.info("接收到的数据,:{}" + id + "====" + longitudeLatitude);

        electronicFenceService.setFenceWay(id, longitudeLatitude);

        return Result.success();

    }


}
